newFormatters.sample = function(name, commands) {
    var content = '';
    initValues(commands);
    content += headersample(name);
    content += footerSample(commands);
    return {
        content: content,
        extension: 'java',
        mimetype: 'text/plain'
    }
};
//存放所有的value内容{"测试值1":"value1","测试值2":"value2"},里面键是value值,值是value对应的java变量名称
var valuesAll = {};
//存放所有的desc内容{"value1":"备注1","value2":"备注2"},里面键是变量名称,值是desc备注内容
var descsAll = {};
//初始化valuesAll
function initValues(commands) {
    valuesAll = {};
    descsAll = {};
    var k = 1;
    for (var i = 0; i < commands.length; i++) {
        var command = commands[i];
        //判断有没有value值,有的话添加到valuesAll中
        if (command.value != undefined && trimall(command.value) != "") {
            if (!valuesAll.hasOwnProperty(command.value)) {
                valuesAll[command.value] = "value" + k;
                if (command.desc != undefined && trimall(command.desc) != "") {
                    descsAll["value" + k] = command.desc;
                }
                k++;
            }
        }
    }
    for (let k in valuesAll) {
        let value = valuesAll[k]; //将原来的value赋值给一个变量
        valuesAll[value] = k; // 为cluster_info赋新key，不能直接使用cluster_info = {cluster_info[k] : k},会报语法错误
        delete valuesAll[k]; // 删除原来的属性
    }
    var newkey = Object.keys(valuesAll).sort();
    var newObj = {}; //创建一个新的对象，用于存放排好序的键值对
    for (var i = 0; i < newkey.length; i++) {
        //遍历newkey数组
        newObj[newkey[i]] = valuesAll[newkey[i]];
        //向新创建的对象中按照排好的顺序依次增加键值对
    }
    valuesAll = newObj;
}


//去除空格
function trimall(str) {
    return str.replace(/\s/g, "");
};
//缩进
function indents1(index) {
    var str1 = '    '
    return index > 1 ? str1 += indents1(str1, --index) : str1;
};
//类名称
function testClassNameSample(testName) {
    var classNameTmp = trimall(testName);
    return classNameTmp.slice(0, 1).toUpperCase() + classNameTmp.slice(1);
};
//函数名称
function testMethodNameSample(testName) {
    return "test" + testClassNameSample(testName);
};
//格式化日期
Date.prototype.Format = function(fmt) {
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "H+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
};
//生成java文件头部
function headersample(name) {
    var className = testClassNameSample(name);
    var methodName = testMethodNameSample(name);
    var time = new Date().Format("yyyy-MM-dd");
    return "/** \n" +
        "*<p>Copyright ® 中国证监会中央监管信息平台版权所有。</p > \n" +
        "*类名:" + className + " \n" +
        "*创建人: Tester   创建时间:" + time + " \n" +
        "*/\n" +
        "package steps;\n" +
        "\n" +
        "import static com.codeborne.selenide.Selenide.*;\n" +
        "import static com.codeborne.selenide.Condition.*;\n" +
        "\n" +
        "import com.codeborne.selenide.Selenide;\n" +
        "import com.codeborne.selenide.SelenideElement;\n" +
        "import static com.codeborne.selenide.Selectors.byLinkText;\n" +
        "\n" +
        "import java.util.Map;\n" +
        "import csrc.common.BaseCase;\n" +
        "import csrc.common.helpers.ExcelDataProvider;\n" +
        "import steps.interfaces.ISteps;\n" +
        "import static common.Setting.host;\n" +
        "\n" +
        "public class " + className + " implements ISteps {\n" +
        indents1(1) + "@Override\n" +
        indents1(1) + "public void runStep(Map<String, String> data) {\n\n" +
        indents1(2) + "//String dataFromExcel = data.get(\"数据内容\");\n\n";
};
//处理录制的命令
function processCommand(command) {
    command.target = command.target.replace(/"/g, "'");
    command.value = command.value.replace(/"/g, "'");
    //打开网页
    if (command.command == "open") {
        return indents1(2) + "open(host(\"" + command.target + "\"));\n";
    }
    //点击操作
    if (command.command == "click") {
        //以双斜杠开头或者以xpath开头,使用xpath定位
        if (command.target.indexOf("//") == 0) {
            return indents1(2) + "$x(\"" + command.target + "\").click();\n";
        }
        //xpath开头需要去掉开头的xpath
        if (command.target.indexOf("xpath") == 0) {
            return indents1(2) + "$x(\"" + command.target.slice(6) + "\").click();\n";
        }
        //link开头需要使用byText,并且去除前面的5个字符
        if (command.target.indexOf("link") == 0) {
            return indents1(2) + "$(byLinkText(\"" + command.target.slice(5) + "\")).click();\n";
        }
        //id开头需要使用#,并且去除前面的3个字符
        if (command.target.indexOf("id") == 0) {
            return indents1(2) + "$(\"#" + command.target.slice(3) + "\").click();\n";
        }
        //css开头,并且去除前面的4个字符
        if (command.target.indexOf("css=") == 0) {
            return indents1(2) + "$(\"" + command.target.slice(4) + "\").click();\n";
        }
    }
    //输入操作
    if (command.command == "type") {
        //以双斜杠开头或者以xpath开头,使用xpath定位
        if (command.target.indexOf("//") == 0) {
            return indents1(2) + "$x(\"" + command.target + "\").setValue(" + findKeyNew(valuesAll, command.value) + ");\n";
        }
        //xpath开头需要去掉开头的xpath
        if (command.target.indexOf("xpath") == 0) {
            return indents1(2) + "$x(\"" + command.target.slice(6) + "\").setValue(" + findKeyNew(valuesAll, command.value) + ");\n";
        }
        //link开头需要使用byText,并且去除前面的5个字符
        if (command.target.indexOf("link") == 0) {
            return indents1(2) + "$(byLinkText(\"" + command.target.slice(5) + "\")).setValue(" + findKeyNew(valuesAll, command.value) + ");\n";
        }
        //id开头需要使用#,并且去除前面的3个字符
        if (command.target.indexOf("id") == 0) {
            return indents1(2) + "$(\"#" + command.target.slice(3) + "\").setValue(" + findKeyNew(valuesAll, command.value) + ");\n";
        }
        //css开头,并且去除前面的4个字符
        if (command.target.indexOf("css=") == 0) {
            return indents1(2) + "$(\"" + command.target.slice(4) + "\").setValue(" + findKeyNew(valuesAll, command.value) + ");\n";
        }
    }
    //校验操作
    if (command.command == "verifyText") {
        //以双斜杠开头或者以xpath开头,使用xpath定位
        if (command.target.indexOf("//") == 0) {
            return indents1(2) + "$x(\"" + command.target + "\").shouldHave(text(" + findKeyNew(valuesAll, command.value) + "));\n";
        }
        //xpath开头需要去掉开头的xpath
        if (command.target.indexOf("xpath") == 0) {
            return indents1(2) + "$x(\"" + command.target.slice(6) + "\").shouldHave(text(" + findKeyNew(valuesAll, command.value) + "));\n";
        }
        //link开头需要使用byText,并且去除前面的5个字符
        if (command.target.indexOf("link") == 0) {
            return indents1(2) + "$(byLinkText(\"" + command.target.slice(5) + "\")).shouldHave(text(" + findKeyNew(valuesAll, command.value) + "));\n";
        }
        //id开头需要使用#,并且去除前面的3个字符
        if (command.target.indexOf("id") == 0) {
            return indents1(2) + "$(\"#" + command.target.slice(3) + "\").shouldHave(text(" + findKeyNew(valuesAll, command.value) + "));\n";
        }
        //css开头,并且去除前面的4个字符
        if (command.target.indexOf("css=") == 0) {
            return indents1(2) + "$(\"" + command.target.slice(4) + "\").shouldHave(text(" + findKeyNew(valuesAll, command.value) + "));\n";
        }
    }
    //编辑内容操作
    if (command.command == "editContent" || command.command == "sendKeys") {
        //以双斜杠开头或者以xpath开头,使用xpath定位
        if (command.target.indexOf("//") == 0) {
            return indents1(2) + "$x(\"" + command.target + "\").sendKeys(" + findKeyNew(valuesAll, command.value) + ");\n";
        }
        //xpath开头需要去掉开头的xpath
        if (command.target.indexOf("xpath") == 0) {
            return indents1(2) + "$x(\"" + command.target.slice(6) + "\").sendKeys(" + findKeyNew(valuesAll, command.value) + ");\n";
        }
        //link开头需要使用byText,并且去除前面的5个字符
        if (command.target.indexOf("link") == 0) {
            return indents1(2) + "$(byLinkText(\"" + command.target.slice(5) + "\")).sendKeys(" + findKeyNew(valuesAll, command.value) + ");\n";
        }
        //id开头需要使用#,并且去除前面的3个字符
        if (command.target.indexOf("id") == 0) {
            return indents1(2) + "$(\"#" + command.target.slice(3) + "\").sendKeys(" + findKeyNew(valuesAll, command.value) + ");\n";
        }
        //css开头,并且去除前面的4个字符
        if (command.target.indexOf("css=") == 0) {
            return indents1(2) + "$(\"" + command.target.slice(4) + "\").sendKeys(" + findKeyNew(valuesAll, command.value) + ");\n";
        }
    }
    //等待可见
    if (command.command == "waitForVisible") {
        //以双斜杠开头或者以xpath开头,使用xpath定位
        if (command.target.indexOf("//") == 0) {
            return indents1(2) + "$x(\"" + command.target + "\").shouldBe(visible);\n";
        }
        //xpath开头需要去掉开头的xpath
        if (command.target.indexOf("xpath") == 0) {
            return indents1(2) + "$x(\"" + command.target.slice(6) + "\").shouldBe(visible);\n";
        }
        //link开头需要使用byText,并且去除前面的5个字符
        if (command.target.indexOf("link") == 0) {
            return indents1(2) + "$(byLinkText(\"" + command.target.slice(5) + "\")).shouldBe(visible);\n";
        }
        //id开头需要使用#,并且去除前面的3个字符
        if (command.target.indexOf("id") == 0) {
            return indents1(2) + "$(\"#" + command.target.slice(3) + "\").shouldBe(visible);\n";
        }
        //css开头,并且去除前面的4个字符
        if (command.target.indexOf("css=") == 0) {
            return indents1(2) + "$(\"" + command.target.slice(4) + "\").shouldBe(visible);\n";
        }
    }
    //切换窗口
    if (command.command == "selectWindow") {
        if (command.target == "win_ser_local") {
            return indents1(2) + "switchTo().window(0);\n";
        }
        if (/win_ser_\d+/.test(command.target)){
            return indents1(2) + "switchTo().window("+command.target.slice(8)+");\n";
        }
    }
    //关闭窗口
    if (command.command == "close") {
       return indents1(2) + "closeWindow();\n";
    }

    return indents1(2) + "//操作: " + command.command + " 目标: " + command.target + " 值 " + command.value + "没有对应能生成的代码\n"
};

//生成java文件底部
function footerSample(commands) {
    var contentnew = "";
    //定义变量
    for (var key in valuesAll) {
        if (descsAll.hasOwnProperty(key)) {
            contentnew += indents1(2) + "// 【" + descsAll[key] + "】\n";
        }
        contentnew += indents1(2) + "String " + key + " = \"" + valuesAll[key] + "\"; //自动生成的 " + valuesAll[key] + " 对应的内容\n";
    }
    contentnew += "\n";
    for (var i = 0; i < commands.length; i++) {
        var command = commands[i];
        contentnew += processCommand(command);
    }
    return contentnew + indents1(1) + "}\n" + "}\n";
};

function findKeyNew(obj, value, compare = (a, b) => a === b) {
  return Object.keys(obj).find(k => compare(obj[k], value));
}
